package org.example.hadoop3.compress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;
import org.apache.hadoop.io.compress.CompressionOutputStream;
import org.apache.hadoop.util.ReflectionUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestCompress {
    @Test
    public void testCompress() throws IOException, ClassNotFoundException {
        String srcFile = "/Users/gaoqiang/dev/xsync.sh";
        String destFile = "/Users/gaoqiang/dev/xsync.sh";
        FileInputStream in = new FileInputStream(new File(srcFile));
        String classCodec = "org.apache.hadoop.io.compress.DefaultCodec";
        Class<?> clazz = Class.forName(classCodec);
        Configuration conf = new Configuration();
        CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(clazz, conf);
        FileOutputStream fos = new FileOutputStream(new File(destFile + codec.getDefaultExtension()));
        CompressionOutputStream codecOut = codec.createOutputStream(fos);
        IOUtils.copyBytes(in, codecOut, conf);
        IOUtils.closeStream(in);
        IOUtils.closeStream(codecOut);
    }

    @Test
    public void testDecompress() throws IOException, ClassNotFoundException {
        String srcFile = "/Users/gaoqiang/dev/xsync.sh.deflate";
        String destFile = "/Users/gaoqiang/dev/xsync11.sh";
        Configuration conf = new Configuration();
        CompressionCodec codec = new CompressionCodecFactory(conf).getCodec(new Path(srcFile));

        FileInputStream in = new FileInputStream(new File(srcFile));
        CompressionInputStream cin = codec.createInputStream(in);

        FileOutputStream fos = new FileOutputStream(new File(destFile));
        //CompressionOutputStream codecOut = codec.createOutputStream(fos);
        IOUtils.copyBytes(cin, fos, conf);
        IOUtils.closeStream(cin);
        IOUtils.closeStream(fos);
    }
}
